using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CMS.Core.DataInterfaces;
using CMS.Data;
using CMS.Core.Domain;
using CMS.Presenters.Presenters;
using CMS.Presenters.Views;

namespace CMS.Web.ListManager
{
    public partial class ListNguoiSuDung : BaseUserControl, INguoiSuDungView
    {
        NguoiSuDungPresenter presenter;
        IList<NguoiSuDung> objIList;

        public ListNguoiSuDung()
        {
            this.presenter = new NguoiSuDungPresenter(this, NHibernateDaoFactory.Instance.GetNguoiSuDungDao());
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            presenter.CancelUpdateEvent += new EventHandler(HandleCancelUpdateEvent);
        }

        private void HandleCancelUpdateEvent(object sender, EventArgs e)
        {
            Response.Redirect(this.BaseUrl + "&action=VIEW");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.gridObj.DataSource = this.objIList;
                this.gridObj.DataBind();
            }
            this.lbnXoa.Attributes.Add("onclick", "return doSetStatus(2)");
        }

        public string getTenPhongBan(string code)
        {
            return presenter.getTenPhongBan(code).TenPhongBan;
        }

        #region Grid Members
        // For swich page
        protected void gridObj_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gridObj.DataSource = this.objIList;
            gridObj.PageIndex = e.NewPageIndex;
            gridObj.DataBind();

        }
        #endregion

        #region INguoiSuDungView Members

        public bool IsValid
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public IList<NguoiSuDung> NguoiSuDungList
        {
            get { return this.objIList; }
            set { this.objIList = value; }
            
        }
        #endregion

        #region Button members
        // For Delete button
        protected void lbnXoa_Click(object sender, EventArgs e)
        {
            string code = "";
            foreach (GridViewRow row in gridObj.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("chkSelect");
                if (cb.Checked)
                {
                    //code = "'" + Convert.ToString(gridObj.DataKeys[row.RowIndex].Value) + "',";
                    code = Convert.ToString(gridObj.DataKeys[row.RowIndex].Value);
                    presenter.ObjToDelete(code);
                    
                }
            }
            presenter.LoadViewFromModel();
            this.gridObj.DataSource = this.NguoiSuDungList;
            gridObj.DataBind();
        }

        // For Add buuton
        protected void lbnThem_Click(object sender, EventArgs e)
        {
            Response.Redirect(this.BaseUrl + "&action=ADD");
            
        }

        // For Update Status button
        protected void lbnHienThi_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gridObj.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("chkSelect");

                if (cb.Checked)
                {
                    string code = Convert.ToString(gridObj.DataKeys[row.RowIndex].Value);
                    presenter.ObjToUpdate(code);

                }
            }
            presenter.LoadViewFromModel();
            this.gridObj.DataSource = this.NguoiSuDungList;
            gridObj.DataBind();
        }
        #endregion

        protected void gridObj_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Pager)
            {
                foreach (Control c in e.Row.Cells[0].Controls)
                {
                    if (c.GetType().ToString().Equals("System.Web.UI.WebControls.PagerTable"))
                    {
                        Table tbl = (Table)c;
                        TableCell cell = new TableCell();
                        cell.Text = "Trang ";
                        cell.HorizontalAlign = HorizontalAlign.Right;
                        tbl.Rows[0].Cells.AddAt(0, cell);
                    }
                }
            }
        }
    }
}